package zhao.tx.eecs.myThread;

import android.util.Log;


public class WatchDog extends Thread implements Runnable {

    private boolean running = false;
    private boolean waiting = false;

    private WatchdogInterface iThreadInterface;

    public void setiThreadInterface ( WatchdogInterface iThreadInterface ) {
        this.iThreadInterface = iThreadInterface;
    }

    private int time = - 1;
    
    private int myTime = 0;

    private int totalTime = - 1;

    public WatchDog ( int time ) {
        this.time = time;
    }

    public WatchDog ( int time, int allTime ) {
        this.time = time;
        this.totalTime = allTime;
    }

    public WatchDog () {
    }
    
    private boolean isSatrt = false;

    @Override
    public synchronized void start () {
        // TODO Auto-generated method stub
        super.start ();
        running = true;
        setSatrt(true);
    }

    
    
    /**
     * �����߳�
     */
    public void suspendThread () {
        myTime = 0;
    	if ( waiting ) { // �ǹ���״̬��ֱ�ӷ���
            return;
        }
        synchronized ( this ) {
            this.waiting = true;
        }
    }

    /**
     * �ָ��߳�
     */
    public void resumeThread () {
        if ( ! waiting ) { // û�й�����ֱ�ӷ���
            return;
        }
        synchronized ( this ) {
            this.waiting = false;
            this.notifyAll ();
        }
    }

    /**
     * ֹͣ�߳�
     */
    public void stopThread () {
        if ( ! running ) { // û��������ֱ�ӷ���
            return;
        }
        synchronized ( this ) {
            running = false;
        }

    }
    
    

    public void stopDo () {
        iThreadInterface.stopDo (); 	
    }
    
    @Override
    public void run () {
        // TODO Auto-generated method stub
        Log.i ( "Time", totalTime + "" );
        if ( time != - 1 ) {

            if ( totalTime != - 1 ) {

                myTime = 0;
                while ( myTime <= totalTime ) {
                    // �̹߳�����˳�����
                	try {
                        Thread.sleep ( time );
                        myTime += 1000;
                    } catch ( InterruptedException e ) {
                        // TODO Auto-generated catch block
                        e.printStackTrace ();
                    }

                    iThreadInterface.stopDo();
                    
                	synchronized ( this ) {
                        if ( ! running ) {
                            break;
                        }
                        if ( waiting ) {
                            try {
                                this.wait ();
                            } catch ( InterruptedException e ) {
                                // TODO Auto-generated catch block
                                e.printStackTrace ();
                            }
                        }
                    }
                    
                }
                if ( isSatrt ) {
                    iThreadInterface.timeGone();    	
                }
            } else {
                while ( true ) {
                    // �̹߳�����˳�����
                	try {
                        Thread.sleep ( time );
                    } catch ( InterruptedException e ) {
                        // TODO Auto-generated catch block
                        e.printStackTrace ();
                    }
                    synchronized ( this ) {
                        if ( ! running ) {
                            isSatrt = false;
                            break;
                        }
                        if ( waiting ) {
                            try {
                                this.wait ();
                            } catch ( InterruptedException e ) {
                                // TODO Auto-generated catch block
                                e.printStackTrace ();
                            }
                        }

                    }

                    iThreadInterface.thraadDoSomehing ();
                    
                }
            }
        } else {
            synchronized ( this ) {
                if ( ! running ) {
                    iThreadInterface.stopDo ();

                    isSatrt = false;
                    return;
                }
                if ( waiting ) {
                    try {
                        this.wait ();
                    } catch ( InterruptedException e ) {
                        // TODO Auto-generated catch block
                        e.printStackTrace ();
                    }
                }

                iThreadInterface.thraadDoSomehing ();
            }
        }
        isSatrt = false;

    }

	public boolean isSatrt() {
		return isSatrt;
	}

	public void setSatrt(boolean isSatrt) {
		this.isSatrt = isSatrt;
	}

}
